package com.vineetmanohar.nitrogen;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * ObjectModel represents a POJO model.
 * 
 * Model maybe loaded from an XML file, YAML, JSON, database or directly built
 * in Java. Once loaded in memory, the Model object represents the data in
 * Hierarchical form and is meant to be passed to the code generation engine.
 * 
 * @author vineetmanohar
 */
public class ArrayModel extends ArrayList<Model> implements WritableModel {
	private static final long serialVersionUID = 1L;
	public Map<String, Model> children = new HashMap<String, Model>();

	public boolean isPrimitive() {
		return false;
	}

	public ObjectModel getObjectModel() {
		throw new NitrogenException(this + " is not an object", null);
	}

	public boolean isObject() {
		return false;
	}

	public boolean isArray() {
		return true;
	}

	public ArrayModel getArrayModel() {
		return this;
	}

	public PrimitiveModel getPrimitive() {
		throw new NitrogenException(this + " is not a primitive", null);
	}

	private Model parent;

	public Model getParent() {
		return parent;
	}

	public void setParent(Model parent) {
		this.parent = parent;
	}

	private String modelName;

	public String getModelName() {
		if (parent != null && parent.isArray()) {
			return parent.getArrayModel().getModelName();
		}

		return modelName;
	}

	public void setModelName(String modelName) {
		this.modelName = modelName;
	}

	public void addChild(WritableModel writableModel) {
		writableModel.setParent(this);
		add(writableModel);
	}

	public int getDepth() {
		return ModelUtils.getDepth(this);
	}
}